package com.example.opengldemo.simple12_01;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.view.View;

import com.example.opengldemo.R;
import com.example.opengldemo.simple12_01.demo.MyObjectLoader;
import com.example.opengldemo.util.LoadUtil;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

class MySurfaceView extends GLSurfaceView 
{
	private final float TOUCH_SCALE_FACTOR = 180.0f/320;
    private SceneRenderer mRenderer;
    
    private float mPreviousY;
    private float mPreviousX;

    float ratio;
	public MySurfaceView(Context context) {
        super(context);
        this.setEGLContextClientVersion(3);
        mRenderer = new SceneRenderer();
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    @Override 
    public boolean onTouchEvent(MotionEvent e) 
    {
        float y = e.getY();
        float x = e.getX();
        switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:
            float dy = y - mPreviousY;
            float dx = x - mPreviousX;
            mRenderer.yAngle += dx * TOUCH_SCALE_FACTOR;
            mRenderer.xAngle+= dy * TOUCH_SCALE_FACTOR;
            requestRender();
        }
        mPreviousY = y;
        mPreviousX = x;
        return true;
    }

	private class SceneRenderer implements Renderer
    {  
		float yAngle;
    	float xAngle;

        MyObjectLoader loader = null ;
        public void onDrawFrame(GL10 gl)
        {   //设置背景色
            GLES30.glClearColor(0.0f,0.0f,0.0f,1.0f);
            //清空深度和颜色缓存
            GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
            MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 2, 100);
            MatrixState.setCamera(0,0,30f,0f,0f,0,0f,1.0f,0.0f);
            
            MatrixState.pushMatrix();
            MatrixState.translate(0, -15f, -25f);
            MatrixState.rotate(yAngle, 0, 1, 0);
            MatrixState.rotate(xAngle, 1, 0, 0);
            if(loader!=null)
            {
                loader.drawSelf(textureId);
            }

            //开启
        	GLES30.glEnable(GL10.GL_SCISSOR_TEST);
        	GLES30.glScissor(0,1080-300,430,300);//0,480-200,230,200);
            GLES30.glClearColor(0.7f,0.7f,0.7f,1.0f);
            GLES30.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

            MatrixState.setProjectFrustum(-0.62f*ratio, 4.18f*ratio, -2.55f, 0.45f, 2, 100);

            MatrixState.setCamera(0, 50f, -30f,0, -2f, -25f,0f,0.0f,-1.0f);

            if(loader!=null)
            {
                loader.drawSelf(textureId);
            }

        	GLES30.glDisable(GL10.GL_SCISSOR_TEST);


            //开启
            GLES30.glEnable(GL10.GL_SCISSOR_TEST);
            GLES30.glScissor(0,1080-600,430,300);//0,480-200,230,200);
            GLES30.glClearColor(0.7f,0.7f,0.7f,1.0f);
            GLES30.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

            MatrixState.setProjectFrustum(-0.62f*ratio, 4.18f*ratio, -2.55f, 2.45f, 2, 100);

            MatrixState.setCamera(30, -50f, 0f,0, -2f, -25f,0f,0.0f,-1.0f);

            if(loader!=null)
            {
                loader.drawSelf(textureId);
            }

            GLES30.glDisable(GL10.GL_SCISSOR_TEST);
            MatrixState.popMatrix();
        }  
        int textureId ;
        public void onSurfaceChanged(GL10 gl, int width, int height) {

        	GLES30.glViewport(0, 0, width, height); 

            ratio = (float) width / height;

            MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 2, 100);

            MatrixState.setCamera(0,0,0,0f,0f,0f,0f,1.0f,0.0f);
        }

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

            GLES30.glEnable(GLES30.GL_DEPTH_TEST);

            GLES30.glEnable(GLES30.GL_CULL_FACE);

            MatrixState.setInitStack();

            MatrixState.setLightLocation(40, 10, 20);

            LoadUtil.getLoadObj(
                    MySurfaceView.this ,
                    "nine/ch_t.obj" ,
                    new LoadUtil.CreateObjectLoader(){
                        @Override
                        public void create(View view, float[] vertexArray, float[] normalArray, float[] texcoorsArray) {
                            loader = new MyObjectLoader(view , vertexArray , normalArray , texcoorsArray ) ;
                        }
                    });

            textureId = TextureUtil.getTextureId(MySurfaceView.this  , R.drawable.was , null , false);
        }
    }
}
